﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Internal/Internal.Master" AutoEventWireup="true"
    Theme="Internal" CodeBehind="MachineTemplate.aspx.cs" Inherits="Landscape.WebApp.Internal.Admin.MachineTemplate" %>

<%@ Register Src="../../Controls/TagItControl.ascx" TagName="TagItControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:HiddenField ID="hfTemplateJSON" runat="server" />
    <telerik:RadToolBar ID="rtbModuleActions" runat="server" Width="100%" EnableShadows="True"
        OnClientButtonClicked="rtbModuleActions_OnClientButtonClicked">
        <Items>
            <telerik:RadToolBarButton runat="server" CommandName="LIST" ImageUrl="~/Images/Search.png"
                Text="Search" CheckOnClick="True" ImagePosition="AboveText" Value="SEARCH">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="NEW" ImageUrl="~/Images/Write.png"
                Text="New" CheckOnClick="True" ImagePosition="AboveText"  Value="NEW">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="SAVE" ImageUrl="~/Images/Floppy.png"
                Text="Save" CheckOnClick="True" ImagePosition="AboveText" Value="SAVE">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="DELETE" ImageUrl="~/Images/Delete_32x32.png"
                Text="Delete" CheckOnClick="True" ImagePosition="AboveText" Value="DELETE">
            </telerik:RadToolBarButton>
        </Items>
    </telerik:RadToolBar>
    <telerik:RadTabStrip ID="rtsTemplateManagement" runat="server" MultiPageID="RadMultiPage1"
        SelectedIndex="2" Width="100%">
        <Tabs>
            <telerik:RadTab runat="server" Text="Search Results" PageViewID="RadPageView1">
            </telerik:RadTab>
            <telerik:RadTab runat="server" Text="Details" PageViewID="RadPageView2">
            </telerik:RadTab>
            <telerik:RadTab runat="server" Text="Constituents" PageViewID="RadPageView3" Selected="True">
            </telerik:RadTab>
        </Tabs>
    </telerik:RadTabStrip>
    <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="2">
        <telerik:RadPageView ID="RadPageView1" runat="server" Selected="true">
            <div class="ContentTabBody">
                <telerik:RadGrid ID="rgSearchResults" runat="server" AllowMultiRowSelection="True"
                    AllowPaging="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None">
                    <ClientSettings>
                        <Selecting AllowRowSelect="True" />
                        <ClientEvents OnCommand="rgSearchResults_OnCommand" OnGridCreated="rgSearchResults_OnGridCreated"
                            OnRowDataBound="rgSearchResults_OnRowDataBound" />
                    </ClientSettings>
                    <MasterTableView>
                        <CommandItemSettings ExportToPdfText="Export to PDF" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridClientSelectColumn FilterControlAltText="Filter column column" HeaderText="#"
                                UniqueName="column">
                            </telerik:GridClientSelectColumn>
                            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter column1 column"
                                HeaderText="Name" UniqueName="column1">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter column2 column"
                                HeaderText="Description" UniqueName="column2">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                            </EditColumn>
                        </EditFormSettings>
                    </MasterTableView><FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid></div>
        </telerik:RadPageView>
        <telerik:RadPageView ID="RadPageView2" runat="server">
            <div class="ContentTabBody">
                <table>
                    <tr style="vertical-align: top;">
                        <td>
                            <asp:HiddenField ID="hfTemplateID" runat="server" />
                            <table>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label1" runat="server" Text="Name :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxName" runat="server">
                                        </telerik:RadTextBox><uc1:TagItControl ID="TagItControl1" runat="server" />
                                        <asp:HiddenField ID="hfItemID" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top; height: 50px">
                                    <td>
                                        <asp:Label ID="Label2" runat="server" Text="Description :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxDiscription" runat="server" TextMode="MultiLine" Height="49px">
                                        </telerik:RadTextBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label4" runat="server" Text="Is Disabled :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsDisabled" runat="server" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </telerik:RadPageView>
        <telerik:RadPageView ID="RadPageView3" runat="server">
            <div class="ContentTabBody">
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="Label10" runat="server" Text="Current Constituents For This Template :"
                                SkinID="ContentHeading"></asp:Label><hr />
                        </td>
                        <td rowspan="2" style="width: 5px; background-color: #dc1ca6">
                        </td>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text="Select Constituents For This Template :"
                                SkinID="ContentHeading"></asp:Label><hr />
                            <asp:HiddenField ID="hfParent" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <telerik:RadTreeView ID="rtvTemplateMachinery" runat="server" MultipleSelect="True"
                                CheckBoxes="True" CheckChildNodes="True" MaxDataBindDepth="5" OnClientNodeExpanded="rtvTemplateMachinery_OnClientNodeExpanded"
                                 OnClientNodeClicked="rtvTemplateMachinery_OnClientNodeClicked" OnClientNodeChecked="rtvTemplateMachinery_OnClientNodeChecked" TriStateCheckBoxes="True">
                            </telerik:RadTreeView>
                        </td>
                        <td valign="top">
                            <telerik:RadGrid ID="rgMachineryData" runat="server" AllowMultiRowSelection="True"
                                AllowPaging="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None">
                                <ClientSettings>
                                    <Selecting AllowRowSelect="True" />
                                    <ClientEvents OnCommand="rgMachineryData_OnCommand" OnGridCreated="rgMachineryData_OnGridCreated" />
                                </ClientSettings>
                                <MasterTableView>
                                    <CommandItemSettings ExportToPdfText="Export to PDF" />
                                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                                        <HeaderStyle Width="20px" />
                                    </RowIndicatorColumn>
                                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                                        <HeaderStyle Width="20px" />
                                    </ExpandCollapseColumn>
                                    <Columns>
                                        <telerik:GridClientSelectColumn FilterControlAltText="Filter column column" HeaderText="#"
                                            UniqueName="column">
                                        </telerik:GridClientSelectColumn>
                                        <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter column1 column"
                                            HeaderText="Name" UniqueName="column1">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter column5 column"
                                            HeaderText="Description" UniqueName="column5">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="Specifications" FilterControlAltText="Filter column2 column"
                                            HeaderText="Specifications" UniqueName="column2">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="Type" FilterControlAltText="Filter column3 column"
                                            HeaderText="Type" UniqueName="column3">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="IsBusinessCritical" FilterControlAltText="Filter column4 column"
                                            HeaderText="Is Business Critical" UniqueName="column4">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="IsEnvironmentCritical" FilterControlAltText="Filter column6 column"
                                            HeaderText="Is Environment Critical" UniqueName="column6">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="IsSafetyCritical" FilterControlAltText="Filter column7 column"
                                            HeaderText="Is Safety Critical" UniqueName="column7">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="IsMandatory" FilterControlAltText="Filter column8 column"
                                            HeaderText="Is Mandatory" UniqueName="column8">
                                        </telerik:GridBoundColumn>
                                    </Columns>
                                    <EditFormSettings>
                                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                        </EditColumn>
                                    </EditFormSettings>
                                </MasterTableView><FilterMenu EnableImageSprites="False">
                                </FilterMenu>
                            </telerik:RadGrid>
                        </td>
                    </tr>
                </table>
            </div>
        </telerik:RadPageView>
    </telerik:RadMultiPage>
    <script type="text/javascript">
        function rtbModuleActions_OnClientButtonClicked(sender, args) {
            processCommands(args.get_item().get_commandName());
            args.get_item().set_checked(false);
        }

        function rgMachineryData_OnGridCreated(sender, args) {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            if (rts.get_selectedTab().get_index() != 0) {
                rts.get_tabs().getTab(0).select();
            }

            loadMachinery(sender);
        }

        function loadMachinery(sender) {
            sender.clearSelectedItems();

            var radgrid = sender.get_masterTableView();
            var selectNodevalue = $get('<%=hfParent.ClientID %>').value.trim();
            var selectedDataItem = null;


            var parentID = null;
            var childType = 'System';
            if (selectNodevalue != '') {
                selectedDataItem = Sys.Serialization.JavaScriptSerializer.deserialize(selectNodevalue)
                parentID = selectedDataItem.MachineItem.ID;
                childType = selectedDataItem.ChildType;
            }

            if ($get('<%=hfTemplateJSON.ClientID %>').value.trim() != '') {
                var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);

                var text = '';
                if (selectNodevalue != null && selectNodevalue != '') {
                    text = 'Select ' + childType.toLowerCase() + 's for ' + selectedDataItem.MachineItem.Name + ' :';
                }
                else {
                    text = 'Select ' + childType.toLowerCase() + 's for this template :';
                }
                $get('<%=Label3.ClientID %>').innerHTML = text;

                Landscape.AdministrativeActions.GetItemsByParentForTemplate(parentID, templateObject.ID, radgrid.get_currentPageIndex(), radgrid.get_pageSize(), function (result, e) {
                    //Bind to radgrid                
                    radgrid.set_dataSource(result);
                    radgrid.dataBind();
                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });

                Landscape.AdministrativeActions.GetItemsCountByParentForTemplate(parentID, templateObject.ID, function (result, e) {
                    radgrid.set_virtualItemCount(result);
                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            }
        }

        function rgMachineryData_OnCommand(sender, args) {

        }

        function rtvMachinaryDetails_OnClientNodeClicked(sender, args) {

        }

        //After search results grid created, add the logic
        function rgSearchResults_OnGridCreated(sender, args) {
            sender.get_masterTableView().set_dataSource('');
            sender.get_masterTableView().dataBind();
        }

        //On any command like paging, sorting
        function rgSearchResults_OnCommand(sender, args) {
            switch (args.get_commandName()) {
                case 'PageSize':
                case 'Page':
                    {
                        performList();
                        break;
                    }
            }
            args.set_cancel(true);
        }

        //On row data bound
        function rgSearchResults_OnRowDataBound(sender, args) {
            var cell = args.get_item().get_cell('column1');
            var anchor = String.format("<a href='#' onclick='return loadTemplateData({0});'>{1}</a>", String.format("\"{0}\"", args.get_dataItem().ID), args.get_dataItem().Name);
            cell.innerHTML = anchor;
        }

        function processCommands(pCmdName) {
            switch (pCmdName) {
                case 'SAVE':
                    {
                        performSave();
                        break;
                    }

                case 'LIST':
                    {
                        performList();
                        break;
                    }

                case 'DELETE':
                    {
                        performDelete();
                        break;
                    }

                case 'NEW':
                    {
                        performNew();
                        break;
                    }
            }
        }

        function performSave() {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            switch (rts.get_selectedTab().get_index()) {
                case 0: //Search Tab
                    {
                        return false;
                        break;
                    }
                case 1: //Details Tab
                    {
                        var item = new Landscape.Template();
                        if ($get('<%=hfTemplateID.ClientID %>').value.trim() != '')
                            item.ID = $get('<%=hfTemplateID.ClientID %>').value.trim();
                        else
                            item.CreatedBy = MASTER_GetCurrentUserCode();

                        item.Name = $find('<%=rtbxName.ClientID %>').get_value();
                        item.Description = $find('<%=rtbxDiscription.ClientID %>').get_value();
                        item.IsDisabled = $get('<%=cbIsDisabled.ClientID %>').checked;

                        item.UpdatedBy = MASTER_GetCurrentUserCode();

                        Landscape.AdministrativeActions.SaveTemplate(item, function (result, e) {
                            if ($get('<%=hfTemplateJSON.ClientID %>').value.trim() == '') {
                                //New Object
                                e.ID = result;
                                $get('<%=hfTemplateJSON.ClientID %>').value = Sys.Serialization.JavaScriptSerializer.serialize(e);
                                loadMachinery($find('<%=rgMachineryData.ClientID %>'));
                            }

                            N_NotifyMessage('Success !!!', 'The template details saved successfully.', 1);
                        },
                        function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); }, item);
                        break;
                    }
                case 2: //Machinery Tab
                    {
                        if ($get('<%=hfTemplateJSON.ClientID %>').value.trim() != '') {
                            var rgMachinery = $find('<%=rgMachineryData.ClientID %>');
                            var parentCurrentItem = null;
                            if ($get('<%=hfParent.ClientID %>').value.trim() != '') {
                                parentCurrentItem = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfParent.ClientID %>').value);
                            }

                            var selectedItems = rgMachinery.get_selectedItems();
                            var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);
                            for (var idx = 0; idx < selectedItems.length; idx++) {
                                var templateMachinery = new Landscape.TemplateMachinery();
                                templateMachinery.ID = '00000000-0000-0000-0000-000000000000';
                                templateMachinery.MachineItemID = selectedItems[idx].get_dataItem().ID;
                                templateMachinery.ParentID = (parentCurrentItem == null ? '00000000-0000-0000-0000-000000000000' : parentCurrentItem.ID);
                                
                                Landscape.AdministrativeActions.AddTemplateMachinery(templateMachinery, templateObject.ID, function (result, e) {
                                    loadTemplateMachines(templateObject.ID);
                                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); })
                            }
                        }
                        else { N_NotifyMessage('Select Template', 'There is no template selected. Please select one template or create a new one to attach the machnery.', 3); }
                        break;
                    }
            }
        }

        function deleteTemplate() {
            var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);
            Landscape.AdministrativeActions.DeleteTemplate(templateObject.ID, 1, function (result, e) {
                N_NotifyMessage('Success !!!', 'Selected template deleted successfully.', 1);
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

        function performDelete() {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            switch (rts.get_selectedTab().get_index()) {
                case 0: //Search
                    {
                        break;
                    }

                case 1: //Details
                    {
                        if ($get('<%=hfTemplateJSON.ClientID %>').value.trim() != '') {

                            N_ConfirmMessage("Are you sure?", "Do you really want to delete this systems template?", 3, deleteTemplate, null);
                        }
                        else { N_NotifyMessage('Select Template', 'There is no template selected. Please select one template to delete.', 3); }
                        break;
                    }
                case 2: //Machinery
                    {
                        if ($get('<%=hfTemplateJSON.ClientID %>').value.trim() != '') {
                            var rtv = $find('<%=rtvTemplateMachinery.ClientID %>');
                            var checkedNodes = rtv.get_checkedNodes();

                            if (checkedNodes.length > 0 && confirm('Do you want to remove the selected machinery from the template?')) {
                                var machineTemplateItems = new Array();
                                for (var idx = 0; idx < checkedNodes.length; idx++) {
                                    if (checkedNodes[idx].get_checkState() == 1) { //0=No Select, 1=Select and 2=Partial Select (3 State CheckBox)
                                        machineTemplateItems.push(checkedNodes[idx].get_attributes().getAttribute('dataitem'));
                                    }
                                }
                                var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);
                                Landscape.AdministrativeActions.RemoveTemplateItems(templateObject.ID, machineTemplateItems, function (result, e) {
                                    loadTemplateMachines(templateObject.ID);
                                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
                            }
                        }
                        else { N_NotifyMessage('Select Template', 'There is no template selected. Please select one template to remove machnery.', 3); }
                        break;
                    }
            }
        }

        function performNew() {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            switch (rts.get_selectedTab().get_index()) {
                case 0: //Search
                    {
                        if (rts.get_selectedTab().get_index() != 1) {
                            rts.get_tabs().getTab(1).select();                            
                        }
                        clrearAll();
                        break;
                    }

                case 1: //Details
                    {
                        clrearAll();
                        break;
                    }
            }
        }

        function clrearAll() {
            $get('<%=hfTemplateID.ClientID %>').value = '';
            $get('<%=hfTemplateJSON.ClientID %>').value = '';
            $get('<%=hfParent.ClientID %>').value = '';
            $find('<%=rtbxName.ClientID %>').set_value('');
            $find('<%=rtbxDiscription.ClientID %>').set_value('');
            $get('<%=cbIsDisabled.ClientID %>').checked = false;
            $find('<%=rtvTemplateMachinery.ClientID %>').get_nodes().clear();
            sender = $find('<%=rgMachineryData.ClientID %>');
            sender.get_masterTableView().set_dataSource('');
            sender.get_masterTableView().dataBind();
        }

        function performList() {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            if (rts.get_selectedTab().get_index() != 0) {
                rts.get_tabs().getTab(0).select();
            }

            $find('<%=rgSearchResults.ClientID %>').clearSelectedItems();

            var radgrid = $find('<%=rgSearchResults.ClientID %>').get_masterTableView();
            var name = '';

            Landscape.AdministrativeActions.GetTemplatesByName(name.trim(), radgrid.get_currentPageIndex(), radgrid.get_pageSize(), function (result, e) {
                //Bind to radgrid                
                radgrid.set_dataSource(result);
                radgrid.dataBind();
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });

            Landscape.AdministrativeActions.GetTemplateCountByName(name.trim(), function (result, e) {
                radgrid.set_virtualItemCount(result);
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

        function loadTemplateData(templateID) {
            var rts = $find('<%=rtsTemplateManagement.ClientID %>');
            if (rts.get_selectedTab().get_index() != 1) {
                rts.get_tabs().getTab(1).select();
            }

            clrearAll();

            Landscape.AdministrativeActions.GetTemplateByID(templateID, function (result, e) {
                $get('<%=hfTemplateID.ClientID %>').value = result.ID;
                $find('<%=rtbxName.ClientID %>').set_value(result.Name);
                $find('<%=rtbxDiscription.ClientID %>').set_value(result.Description);
                $get('<%=cbIsDisabled.ClientID %>').checked = result.IsDisabled;
                $get('<%=hfTemplateJSON.ClientID %>').value = Sys.Serialization.JavaScriptSerializer.serialize(result);

                loadTemplateMachines(result.ID);

                loadMachinery($find('<%=rgMachineryData.ClientID %>'));

            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            return false;
        }

        function loadTemplateMachines(pTemplateID) {

            Landscape.AdministrativeActions.GetTemplateMachineryListByParent(pTemplateID, null, function (result2, e) {   
                         
                addToTreeViewNode($find('<%=rtvTemplateMachinery.ClientID %>'), result2);
            }, function (result2, e) { N_NotifyMessage('Error !!!', result2.get_message(), 2); });
        }

        function addToTreeViewNode(pParentNode, pResultArray) {
            pParentNode.get_nodes().clear();
            if (pResultArray.length > 0) {
                for (var i = 0; i < pResultArray.length; i++) {
                    var node = new Telerik.Web.UI.RadTreeNode();
                    node.set_text(pResultArray[i].MachineItem.Name);
                    node.set_value(pResultArray[i].ID);
                    node.get_attributes().setAttribute('dataitem', pResultArray[i]);
                    var node = pParentNode.get_nodes().add(node);
                }
            }
        }

        function rtvTemplateMachinery_OnClientNodeExpanded(sender, args) {

        }

        function rtvTemplateMachinery_OnClientNodeClicked(sender, args) {
            var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);
            sender.trackChanges();
            args.get_node().get_nodes().clear();
            sender.commitChanges();

            Landscape.AdministrativeActions.GetTemplateMachineryListByParent(templateObject.ID, args.get_node().get_value(), function (result, e) {
                addToTreeViewNode(args.get_node(), result);
                args.get_node().expand();
                $get('<%=hfParent.ClientID %>').value = Sys.Serialization.JavaScriptSerializer.serialize(args.get_node().get_attributes().getAttribute('dataitem'));

                loadMachinery($find('<%=rgMachineryData.ClientID %>'));
            }, function (result2, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

        function rtvTemplateMachinery_OnClientNodeChecked(sender, args) {
            var templateObject = Sys.Serialization.JavaScriptSerializer.deserialize($get('<%=hfTemplateJSON.ClientID %>').value);            
            if (args.get_node().get_nodes().get_count() == 0) {
                //Add nodes
                Landscape.AdministrativeActions.GetTemplateMachineryListByParent(templateObject.ID, args.get_node().get_value(), function (result, e) {
                    addToTreeViewNode(args.get_node(), result);
                    args.get_node().expand();
                    $get('<%=hfParent.ClientID %>').value = Sys.Serialization.JavaScriptSerializer.serialize(args.get_node().get_attributes().getAttribute('dataitem'));

                    loadMachinery($find('<%=rgMachineryData.ClientID %>'));

                    for (var idx = 0; idx < args.get_node().get_nodes().get_count(); idx++) {
                        args.get_node().get_nodes().getNode(idx).check();
                        rtvTemplateMachinery_OnClientNodeChecked(sender, new Telerik.Web.UI.RadTreeNodeEventArgs(args.get_node().get_nodes().getNode(idx), args.get_domEvent()));
                    }

                }, function (result2, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            }
        }      
    </script>
</asp:Content>
